package com.livestocksolutions.util;

/**
 * Store an Item, that can be stored directly in a ComboBox.
 * @param <T> The Object to store in this ItemList
 *
 * @author Adam Parr
 * @.modified 16 October 2012
 */
public class ItemList<T> {
	/**
	 * The Object associate with this ItemList
	 */
	protected T item = null;
	/**
	 * The String of what should be returned when an actual Object of T is not
	 * stored in this Object.
	 */
	protected String value = "";
	/**
	 * Constructor
	 * @param value The Value that will be returned when this Objects toString()
	 *               method is called.
	 */
	public ItemList(String value) {
		this.value = value!=null?value:"";
	}
	/**
	 * Constructor
	 * @param item The Object to store in this ItemList
	 */
	public ItemList(T item){
		this.item = item;
	}
	
	/**
	 * Get the Object stored in this ItemList.
	 * @return The Object or subclass stored in this ItemList.
	 */
	public T getItem(){
		return item;
	}
	
	/**
	 * Check to see if this Items Object is the same.
	 * @param itemList Check that the Object is the same.
	 * @return <tt>true</tt> if the ItemList Objects are the same, <tt>false</tt> otherwise.
	 */
	public boolean equals(ItemList<?> itemList){
		if(item==null){
			if(!value.equals("")){
				return value.equals(itemList.value);
			}
			return itemList.item==null;
		}
		return item.equals(itemList.item);
	}

	@Override
	public boolean equals(Object itemList){
		if(itemList instanceof ItemList<?>){
			return equals((ItemList<?>)itemList);
		}
		return super.equals(itemList);
	}
	
	@Override
	public int hashCode() {
		return !value.equals("")?item.hashCode():value.hashCode();
	}
	
	@Override
	public String toString(){
		return item.toString();
	}
}
